<?php 

	class Admin_DocumentController extends Admin_BaseController {

		public function getIndex($id = null) {
			if($id != 'root') {
				$parent = $id;
				$folderPathArray = array();

				do {
					$docs = Documents::where('id', '=', $parent)->first();
					$parent = $docs->parent_id;
					$folderPathArray[$docs->id] = $docs->filename;
				} while($parent != 'root');

				$folderPathArray['root'] = "Racine";

				$breadcrumb = array_reverse($folderPathArray, true);
			}
			else {
				$breadcrumb = array('root' => 'Racine');
				$path = "";
			}

			$documents = Documents::where('parent_id', '=', $id)->orderBy('type', 'DESC')->orderBy('title')->paginate(20);
			return View::make("admin.documents.index", compact('id', 'breadcrumb', 'documents'));
		}

		public function getAddDocument($id = null) {
			return View::make("admin.documents.addfile", compact('id'));
		}

		public function getAddFolder($id = null) {
			return View::make("admin.documents.addfolder", compact('id'));
		}

		public function postAddDocument($id = null) {
			$rules = array(
				'titre' => 'required|between:1,255'
			);

			$validator = Validator::make(Input::all(), $rules);

			if ($validator->fails()) {
				return Redirect::back()->withInput()->withErrors($validator);
			}

			if(!Input::hasFile('file')) {
				return Redirect::back()->withInput()->withErrors($validator);
			}

			$destinationPath = public_path()."/documents/";
			$potentialPath = "";

			if($id != 'root') {
				$parent = $id;
				$folderPathArray = array();

				do {
					$docs = Documents::where('id', '=', $parent)->first();
					$parent = $docs->parent_id;
					array_push($folderPathArray, $docs->filename);
				} while($parent != 'root');

				$folderPathArray = array_reverse($folderPathArray);

				$potentialPath = implode('/', $folderPathArray).'/';
			}
			$filename = Input::file('file')->getClientOriginalName();
			$path = $destinationPath.$potentialPath.$filename;

			if(!File::exists($path)) {
				Input::file('file')->move($destinationPath.$potentialPath, $filename);

				$document = new Documents;
				$document->title = Input::get('titre');
				$document->filename = $filename;
				$document->path = 'documents/'.$potentialPath.$filename;
				$document->parent_id = $id;
				$document->type = 0;
				$document->user_id = Auth::user()->id;
				$document->save();

				return View::make('admin.documents.file.ok', compact('id'));
			}
			else {
				return View::make('admin.documents.file.ko', compact('id'));
			}
		}

		public function postAddFolder($id = null) {
			$rules = array(
				'titre' => 'required|between:1,255'
			);

			$validator = Validator::make(Input::all(), $rules);

			if ($validator->fails()) {
				return Redirect::back()->withInput()->withErrors($validator);
			}

			$destinationPath = public_path()."/documents/";
			$potentialPath = "";

			if($id != 'root') {
				$parent = $id;
				$folderPathArray = array();

				do {
					$docs = Documents::where('id', '=', $parent)->first();
					$parent = $docs->parent_id;
					array_push($folderPathArray, $docs->filename);
				} while($parent != 'root');

				$folderPathArray = array_reverse($folderPathArray);

				$potentialPath = implode('/', $folderPathArray).'/';
			}

			$path = $destinationPath.$potentialPath.Input::get('titre');

			if(!File::exists($path)) {
				File::makeDirectory($path);

				$document = new Documents;
				$document->title = Input::get('titre');
				$document->filename = Input::get('titre');
				$document->parent_id = $id;
				$document->type = 1;
				$document->user_id = Auth::user()->id;
				$document->save();

				$id = $document->id;

				return View::make('admin.documents.folder.ok', compact('id'));
			}
			else {
				return View::make('admin.documents.folder.ko', compact('id'));
			}
		}

		public function getDeleteFile($id = null) {
			$document = Documents::where('id', '=', $id);
			$doc = $document->first();

			File::delete(public_path().'/'.$document->first()->path);

			$document->delete();

			return Redirect::route('admin-docs', array('id' => $doc->parent_id));
		}
	}